package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"jwgin/service"
)

// 权限控制
func RBAC() gin.HandlerFunc {
	return func(context *gin.Context) {
		if Account, bools := context.Get("RoleAccount"); bools == true && Account == "jwcode" {
			context.Next()
		} else {
			roleid, bols := context.Get("RoleID")
			if !bols {
				context.AbortWithStatusJSON(403, gin.H{"message": "非法操作"})
			}
			access, err := service.E.Enforce(fmt.Sprintf("%v", roleid), context.Request.RequestURI, context.Request.Method)
			if err != nil || !access {
				context.AbortWithStatusJSON(403, gin.H{"message": "没有权限"})
			} else {
				context.Next()
			}
		}

	}
}
